package com.example.testpic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.example.testpic.R.color;
import com.example.vidget.TouchImageView;
import com.example.vidget.TouchImageViewPager;
import com.example.vidget.TouchImageViewPager.CurrTouchImageViewListener;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ImagePreviewActivity extends Activity {

	private TouchImageViewPager viewPager;
	private TextView imageTitleView;
	private ImageViewPagerAdapter imageViewPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.image_preview);
		imageTitleView = (TextView) findViewById(R.id.image_title);
		imageTitleView.setText("1/" + AlbumListActivity.map.size());
		viewPager = (TouchImageViewPager) findViewById(R.id.image_preview_tool);
		imageViewPagerAdapter = new ImageViewPagerAdapter(this);
		imageViewPagerAdapter.setImagePathList(AlbumListActivity.pathList);
		viewPager.setAdapter(imageViewPagerAdapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				imageTitleView.setText((arg0 + 1) + "/"
						+ imageViewPagerAdapter.getCount());
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		viewPager.setOnCurrTouchImageViewListener(new CurrTouchImageViewListener() {
			
			@Override
			public View onGetCurrTouchImageView(int pos) {
				// TODO Auto-generated method stub
				if(imageViewPagerAdapter!=null){
					return imageViewPagerAdapter.getItemView(pos);
				}
				return null;
			}
		});
	}

	class ImageViewPagerAdapter extends PagerAdapter {

		private List<String> imagePaths;
		private Context context;
		private Map<Integer, View> tmpMap;

		public ImageViewPagerAdapter(Context context) {
			this.context = context.getApplicationContext();
			tmpMap = new HashMap<Integer, View>();
		}

		public void setImagePathList(List<String> imagePaths) {
			this.imagePaths = imagePaths;
		}

		public View getItemView(int pos){
			return tmpMap.get(pos);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return imagePaths.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj) {
			// TODO Auto-generated method stub
			return view == (View) obj;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			tmpMap.remove(position);
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
//			TouchImageView touchImageView = new TouchImageView(context);
		    com.example.vidget.TouchImageView touchImageView = new TouchImageView(context);
			try {
				Bitmap bitmap = Bimp.revitionImageSize(
						imagePaths.get(position), 1000, 1000);
				touchImageView.setImageBitmap(bitmap);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			} catch (OutOfMemoryError e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			((ViewPager) container).addView(touchImageView, 0);
			tmpMap.put(position, touchImageView);
			return touchImageView;
		}

	}
}
